package com.disney.dolcn.invitemail.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.disney.dolcn.core.common.http.HttpTransportImpl;
import com.disney.dolcn.core.common.http.IHttpTransport;
import com.disney.dolcn.core.database.SqlDealer;
import com.disney.dolcn.invitemail.bean.Coupon;
import com.disney.dolcn.invitemail.util.InviteConstant;
import com.disney.dolcn.invitemail.util.MailService;

/**
 * 
 * @author hawang
 * 
 */
public final class InviteUserDao {
	private SqlDealer sqlDealer = null;

	/**
	 * {@link CommentDao} constructor.
	 * 
	 * @param dealer
	 *            {@link SqlDealer}
	 */
	public InviteUserDao(SqlDealer dealer) {
		this.sqlDealer = dealer;
	}

	public InviteUserDao() {

	}

	/**
	 * 添加新的邮件纪录
	 * 
	 * @param name
	 * @param email
	 * @param emails
	 * @throws SQLException
	 */
	public void addUser(String name, String email, String emails)
			throws SQLException {

		StringBuffer sb = new StringBuffer();
		sb.append(InviteConstant.WEB_CONFIG);
		sb.append("?service=").append(InviteConstant.SERVICE);
		sb.append("&action=").append(InviteConstant.ADD_ACTION);

		sb.append("&encode=").append("1");
		sb.append("&paramcode=").append("GBK");
		sb.append("&param=");
		StringBuffer paramMq = new StringBuffer();
		paramMq.append(name);
		paramMq.append("|||").append(email);
		paramMq.append("|||").append(emails);

		IHttpTransport transport = new HttpTransportImpl(sb.append(
				java.net.URLEncoder.encode(paramMq.toString())).toString());
		String result = transport.get();
		if (result == null || !result.trim().equals("success")) {
			throw new SQLException("insertEmailUserDetail");
		}

		// sqlDealer.execute(
		// "insert into t_intive_user(name,email,Intive_emails ,join_time)
		// values('"+name+"','"+email+"','"+emails+"',now())");
	}

	/**
	 * 更改一条记录的邮件发送状态
	 * 
	 * @param ids
	 * @throws SQLException
	 */
	public void updateUser(String ids) throws SQLException {
		String id_[] = ids.split(",");
		StringBuffer paramMq = new StringBuffer();

		for (int i = 0; i < 20; i++) {

			if (i == 0) {
				paramMq.append(id_[i]);
			} else {
				if (i >= id_.length) 
					paramMq.append("|||").append("0");
				else
					paramMq.append("|||").append(id_[i]);
			}

		}

		StringBuffer sb = new StringBuffer();
		sb.append(InviteConstant.WEB_CONFIG);
		sb.append("?service=").append(InviteConstant.SERVICE);
		sb.append("&action=").append(InviteConstant.UPDATE_ACTION);

		sb.append("&encode=").append("1");
		sb.append("&param=");

		IHttpTransport transport = new HttpTransportImpl(sb.append(
				java.net.URLEncoder.encode(paramMq.toString())).toString());
		 
		String result = transport.get();
		if (result == null || !result.trim().equals("success")) {
			throw new SQLException("insertEmailUserDetail");
		}
		// sqlDealer.execute("update t_intive_user set intive_send=1 where id
		// in("+ids+")");
	}

	/**
	 * 提取未发送的邮件地址
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List getListUser() throws SQLException {
		return sqlDealer
				.executeQuery("select * from t_intive_user where intive_send=0");
	}
	/**
	 * 提取未送出的优惠卷
	 * @return
	 * @throws SQLException
	 */
	public List getCouponList() throws SQLException { 
		List list=new ArrayList();
		List l_1=setCoupon(sqlDealer
		.executeQuery("select * from t_intive_coupon where info=1"));
		//System.out.println("######get 10$ coupon list "+l_1.size());
		list.add(0,l_1);
		
		List l_2=setCoupon(sqlDealer
				.executeQuery("select * from t_intive_coupon where info=2"));
		list.add(1,l_2);
		//System.out.println("######get new Game coupon list "+l_2.size());
		
		List l_3=setCoupon(sqlDealer
				.executeQuery("select * from t_intive_coupon where info=3"));
		list.add(2,l_3);
		//System.out.println("######get deskTop coupon list "+l_3.size());
		return list;
	}
	/**
	 * 将数据库获得数据 转化为 优惠卷对象
	 * @param list
	 * @return
	 */
	public List setCoupon(List l)
	{
		List list=new ArrayList();
		if(l==null || l.size()<1) return null;
		for(int i=0;i<l.size();i++)
		{
			HashMap map=(HashMap)l.get(i);
			Coupon c=new Coupon();
			c.setId(map.get("id").toString());
			c.setInfo(map.get("info").toString());
			c.setName(map.get("name").toString());
			list.add(c);
		}
		return list;
	}
	/**
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public void upadteCoupon(String id)throws   SQLException
	{
		StringBuffer sb = new StringBuffer();
		sb.append(InviteConstant.WEB_CONFIG);
		sb.append("?service=").append(InviteConstant.SERVICE);
		sb.append("&action=").append(InviteConstant.UPDATE_COUPON);

		sb.append("&encode=").append("1");
		sb.append("&paramcode=").append("GBK");
		sb.append("&param=");
		 ///update t_intive_coupon set intive_send=1 where id=?

		IHttpTransport transport = new HttpTransportImpl(sb.append(
				java.net.URLEncoder.encode(id)).toString());
		String result = transport.get();
		if (result == null || !result.trim().equals("success")) {
			throw new SQLException("insertEmailUserDetail");
		}
	}
	/**
	 * 
	 * @param username
	 * @param email
	 * @return
	 * @throws SQLException
	 */
	public int getListUser(String username,String email) throws SQLException {
		List l= sqlDealer
				.executeQuery("select * from t_intive_user where name='"+username+"' and  intive_emails like'%"+email+"%' and intive_send=1 ");
		if(l==null || l.size()<1)return 0;
		return l.size();
	}
	
	public static void main(String sendmail[]) {
		String ids="0,28,27";
		String id_[] = ids.split(",");
		StringBuffer paramMq = new StringBuffer();

		for (int i = 0; i < 20; i++) {
			System.out.println(i);
			if (i == 0) {
				paramMq.append(id_[i]);
			} else {
				if (i >= id_.length)
					paramMq.append("|||").append(i);
				else
					paramMq.append("|||").append(id_[i]);
			}

		}
		}
}
